#include "pickel.h"

PicKel::PicKel(QWidget *parent)
	: QMainWindow(parent), ui(new Ui::PicKelClass)
	{
		QDir notesDir(QDir::toNativeSeparators(QDir::homePath()+"/.picKel/notes"));
		if (!notesDir.exists())
			notesDir.mkpath(notesDir.absolutePath());

		ui->setupUi(this);
		ui->menu_View->insertAction(ui->action_Toolbar, ui->toolBar->toggleViewAction());
		delete ui->action_Toolbar;

		/*	*** TODO ***
		 *
		 *	Add (more) note actions to tray icon menu.
		*/
		trayIcon = new QSystemTrayIcon(QIcon(":/icons/im-status-message-edit.png"), this);
		trayIcon->setContextMenu(ui->menu_File);
		trayIcon->show();

		settingsDiag = new PSettingsDialog(this);
		cabinet = new NotesCabinet(this);
		setCentralWidget(cabinet);

		connect(settingsDiag, SIGNAL(updateGUI()), this, SIGNAL(updateGUI()));
		connect(this, SIGNAL(updateGUI()), cabinet, SLOT(updateGUI()));

		connect(ui->action_Preferences, SIGNAL(triggered()), settingsDiag, SLOT(show()));
		connect(ui->action_About, SIGNAL(triggered()), this, SLOT(about()));
		connect(ui->actionAbout_Qt, SIGNAL(triggered()), this, SLOT(aboutQt()));
		connect(ui->action_Quit, SIGNAL(triggered()), qApp, SLOT(quit()));
		connect(ui->action_Side_Panel, SIGNAL(triggered(bool)), this, SLOT(showNoteBooks(bool)));

		connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconTriggered(QSystemTrayIcon::ActivationReason)));
	}

PicKel::~PicKel()
	{
		delete ui;
	}

void PicKel::about()
	{
		QMessageBox::about(this,
						   tr("About PicKel"),
						   tr("<h3>PicKel</h3>" \
							  "<p>" \
							  "<b>Version:</b> v1.0.0<br/>" \
							  "<b>License:</b> GPL v3" \
							  "</p>" \
							  "<p>" \
							  "PicKel is a simple note-taking program aimed at organization and ease-of-use. " \
							  "It uses normal HTML files for the storing and naming scheme of notes. " \
							  "It is written in C++ and makes use of the Qt GUI toolkit." \
							  "</p>"));
	}

void PicKel::aboutQt()
	{
		QMessageBox::aboutQt(this, tr("About Qt"));
	}

void PicKel::closeEvent(QCloseEvent *event)
{
	if (trayIcon->isVisible())
	{
		event->ignore();
		hide();
	}
}

void PicKel::iconTriggered(QSystemTrayIcon::ActivationReason reason)
	{
		if (reason == QSystemTrayIcon::Trigger)
			(isHidden()) ? show() : hide();
	}

QAction *PicKel::notesAction(NoteActions a) const
	{
		switch (a)
		{
			case NewNote:
				return ui->action_New_Note;
				break;
			case NewNoteBook:
				return ui->actionNew_Note_Book;
				break;
			case RenameNote:
				return ui->action_Rename_Note;
				break;
			case DeleteNote:
				return ui->action_Delete_Note;
				break;
			case CloseNote:
				return ui->action_Close_Note;
				break;
		}
	}

QSettings *PicKel::settingsConf() const
	{
		return settingsDiag->settingsConf();
	}

void PicKel::showNoteBooks(bool x)
	{
		cabinet->noteBooks()->setVisible(x);
	}
